<template>
  <div>
    <h1>三级分类页面</h1>
    <p></p>
    <el-switch v-model="draggable" active-color="#13ce66" inactive-color="#ff4949">
    </el-switch>
    <el-tree
      node-key="catId"
      :default-expanded-keys="defaultExpandedKeys"
      :expand-on-click-node="false"
      @node-drop="nodeDrop"
      :allow-drop="allowDrop"
      :draggable="draggable"
      :data="data"
      :props="defaultProps"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button type="text" size="mini" @click="() => append(data)">
            Append
          </el-button>
          <el-button type="text" size="mini" @click="() => update(data)">
            Update
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="categroy">
        <el-form-item label="活动名称" label-width="100px">
          <el-input v-model="categroy.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = false;
            save();
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultExpandedKeys:[],
      draggable:false,
      updateNodes: [],
      pid: 0,
      catLevel: 1,
      data: [],
      categroy: {
        name: "",
      },
      level: 1,
      dialogFormVisible: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    nodeDrop(draggingNode, dropNode, type) {
       console.log(draggingNode, dropNode, type);
      if (type == "before" || type == "after") {
        this.pid = dropNode.data.parentCid;
        let flag = false;
        let updateChildrenNode = null;
        if (draggingNode.data.catLevel != dropNode.data.catLevel) {
          this.level = dropNode.data.catLevel;
          flag = true;
          updateChildrenNode = draggingNode;
        }
        // 收集
        if (this.pid == 0) {
          this.getupdateNodes(dropNode.parent.data, flag, updateChildrenNode);
        } else {
          this.getupdateNodes(
            dropNode.parent.data.children,
            flag,
            updateChildrenNode
          );
        }
      } else if (type == "inner") {
        this.pid = dropNode.data.catId;

        this.level = dropNode.data.catLevel + 1;
        //收集数据
        this.getupdateNodes(
          dropNode.parent.data.children,
          false,
          updateChildrenNode
        );
      }
      this.$http({
        url: this.$http.adornUrl(`/product/category/updateNodes`),
        method: "post",
        data: this.$http.adornData(this.updateNodes, false),
      }).then(({ data }) => {
        console.log(data);
        if (data.code == 0) {
          this.$message({
            message: "保存 成功",
            type: "success",
          });
          this.updateNodes = [];
          this.getDataList();
        }
      });
    },
    getupdateNodes(nodes, flag, node, level = this.level, pid = this.pid) {
      console.log(nodes);

      for (let index = 0; index < nodes.length; index++) {
        this.updateNodes.push({
          catId: nodes[index].catId,
          name: nodes[index].name,
          parentCid: this.pid,
          catLevel: this.level,
          sort: index,
        });
      }

      if (flag) {
        if (node != null) {
          this.getupdateNodes(
            node.data.children,
            false,
            null,
            this.level + 1,
            node.data.catId
          );
        }
      }
    },
    //draggingNode  被拖拽的对象
    //dropNode 目标对象
    // type 类型
    allowDrop(draggingNode, dropNode, type) {
      // console.log(draggingNode, dropNode, type);
      this.getContlevel(draggingNode);
      //测试 被拖拽的层级
      // console.log(this.catLevel)
      //alert(this.catLevel);
      if (type == "next" || type == "prev") {
        if (this.catLevel == 1) return true;
        if (this.catLevel == 2 && dropNode.data.catLevel != 3) return true;
        if (this.catLevel == 3 && dropNode.data.catLevel == 1) return true;
      } else if (type == "innner") {
        if (this.catLevel == 1 && dropNode.data.catLevel != 3) return true;
        if (this.catLevel == 2 && dropNode.data.catLevel == 1) return true;
      }

      return false;
    },
    //被拖拽的对象
    getContlevel(node) {
      this.catLevel = 1;

      if (node.childNodes.length > 0) {
        this.catLevel = 2;
        for (var i in node.childNodes) {
          if (node.childNodes[i].childNodes.length > 0) {
            this.catLevel = 3;
            return;
          }
        }
      }
    },
    remove(node, data) {
      var ids = [];
      ids.push(node.data.catId);
      console.log(node, data);
      this.$http({
        url: this.$http.adornUrl(`/product/category/delete`),
        method: "post",
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        console.log(data);
        this.getDataList();
      });
    },
    save() {
      let mothurl = "save";
      if (this.categroy.catId) mothurl = "update";
      this.$http({
        url: this.$http.adornUrl(`/product/category/${mothurl}`),
        method: "post",
        data: this.$http.adornData(this.categroy, false),
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message({
            message: "保存 成功",
            type: "success",
          });
          this.defaultExpandedKeys= [this.categroy.parentCid]
          this.categroy = {};
          this.getDataList();
        }
        console.log(data);
      });
    },
    getDataList() {
      this.$http({
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        this.data = data;
      });
    },
    append(data) {
      this.dialogFormVisible = true;
      this.categroy.parentCid = data.catId;
      this.categroy.catLevel = data.catLevel + 1;
      this.categroy.showStatus = 1;
      this.categroy.sort = 0;
    },
    update(node) {
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${node.catId}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.categroy = data.category;
        this.dialogFormVisible = true;
      });
    },
  },
};
</script>